import Vue from 'vue'

Vue.filter('timeshow', time_str => {
  var now = new Date()
  var date = new Date(time_str)
  // 计算时间间隔，单位为分钟
  var inter = parseInt((now.getTime() - date.getTime()) / 1000 / 60)
  if (inter === 0) {
    return '刚刚'
  }
  // 多少分钟前
  else if (inter < 60) {
    return inter.toString() + '分钟前'
  }
  // 多少小时前
  else if (inter < 60 * 24) {
    return parseInt(inter / 60).toString() + '小时前'
  } else {
    return (
      date.getFullYear() +
      '-' +
      (date.getMonth() + 1).toString().padStart(2, '0') +
      '-' +
      date
        .getDate()
        .toString()
        .padStart(2, '0') +
      ' ' +
      date
        .getHours()
        .toString()
        .padStart(2, '0') +
      ':' +
      date
        .getMinutes()
        .toString()
        .padStart(2, '0')
    )
  }
})

Vue.filter('videoTime', time => {
  let h = Math.floor(time / 3600)
  let m = Math.floor((time % 3600) / 60)
  let s = Math.floor(time % 60)
  h = h >= 10 ? h : '0' + h
  m = m >= 10 ? m : '0' + m
  s = s >= 10 ? s : '0' + s
  return h === '00' ? m + ':' + s : h + ':' + m + ':' + s
})

Vue.filter('formatPic', (pic, type) => {
	if (/.gif/.test(pic) || !/sinaimg.cn/.test(pic)) return pic
  const rePic = pic.replace('http://', 'https://')
  return rePic.replace(/large|mw1024/, type)
})